package cn.demoncat.util.io.entity;

import java.io.PrintWriter;

import cn.demoncat.util.lang.entity.LineReadCallback;

/**
 * PrintWriter行读取器
 * 
 * 功能：将读取到的行，直接写出到PrintWriter
 * 
 * @author 延晓磊
 *
 * @since 2019年7月1日
 */
public class WriteLineReadCallback extends LineReadCallback {

	protected PrintWriter writer;

	/**
	 * 创建
	 */
	public WriteLineReadCallback(PrintWriter writer) {
		super();
		this.writer = writer;
	}

	@Override
	public void readImpl(String line){
		toWrite(line);
	}

	@Override
	public void readErrorImpl(String line) {
		toWrite(line);
	}

	@Override
	public void readWarnImpl(String line) {
		toWrite(line);
	}
	
	@Override
	public void readFinish() {
		// 写出完成标记
		toWrite(READ_FINISH);
	}

	/**
	 * 输出
	 * 
	 * 注：输出后直接刷新缓存区让立即输出
	 * 
	 * @param line
	 * 
	 * @author 延晓磊
	 *
	 * @since 2019年7月8日
	 */
	protected void toWrite(String line) {
		writer.println(line);
		writer.flush();
	}
	
	public PrintWriter getWriter() {
		return writer;
	}

	public void setWriter(PrintWriter writer) {
		this.writer = writer;
	}
	
}
